package com.nega.commons.shiro;

import com.nega.commons.model.User;
import com.nega.modules.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;
 
/**
 * @author Garc
 *
 */
@Slf4j
public class UserAuthorizingRealm extends AuthorizingRealm {

    @Resource
    private UserService userService;

    /**
     * 鉴权
     * @param principals
     * @return AuthorizationInfo
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        if (principals == null) {
            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
        }
        User user = (User) getAvailablePrincipal(principals);
        Set<String> permissions = new HashSet<>();
        // 权限
        permissions.add("/mem/index");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(permissions);
        return info;
    }
 
    /**
     * 认证
     * @param token
     * @return AuthenticationInfo
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken uToken = (UsernamePasswordToken) token;
        String userName = uToken.getUsername();
        String password = DigestUtils.md5DigestAsHex(new String(uToken.getPassword()).getBytes());

        User user = userService.getUserByName(userName);

        if (user == null) {
            throw new UnknownAccountException("ER-99");
        }

        if (!user.getPassword().equals(password)){
            throw new IncorrectCredentialsException("ER-02");
        }

        if (!user.getStatus()){
            throw new DisabledAccountException("ER-03");
        }

        return new SimpleAuthenticationInfo(user, user.getPassword(), this.getName());
    }
 
    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        HashedCredentialsMatcher credentialsMatche = new HashedCredentialsMatcher();
        credentialsMatche.setHashAlgorithmName("MD5");
        credentialsMatche.setHashIterations(1);
        super.setCredentialsMatcher(credentialsMatche);
    }
}
